﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IridiumFlares;
using System.Text.RegularExpressions;

namespace IridiumParser
{
    public class FlareParser
    {
        #region HTML Parsing
        private static Match match = null;

        /*Match everything between <td>...</td> and ignore <td attributes>*/
        private const string RegAttr = @"<td.*?>([\s\S]+?)</td>";
        /*Match time in the format xx:xx:xx*/
        private const string RegTime = @"(?<time>(\d{2}:){2}\d{2})";
        /*Match one up to three digit*/
        private const string RegDegree = @"(?<degree>\d{1,3})";
        /*Match 1-3 digit number of Iridium Flare Satellite*/
        private const string RegSatelliteNo = @"Iridium\s*(?<sat>\d{1,3})";

        public static Flare parseHTML(String flareHTML)
        {
            int pointer = 0;

            String[] strings = getStrings(flareHTML);
            DateTime tempTime = parseDateTime(strings[pointer++], strings[pointer++]);
            if (tempTime == null) return null;
            int tempIntensity = 0;
            int tempAltitude = 0;
            int tempAzimuth = 0;
            int tempSatelliteNo = 0;
            try
            {
                tempIntensity = parseIntensity(strings[pointer++]);
                tempAltitude = parseDegree(strings[pointer++]);
                tempAzimuth = parseDegree(strings[pointer++]);
                tempSatelliteNo = parseSatelliteNo(strings[7]);
            }
            catch (Exception e)
            {
                return null;
            }

            Flare flare = new Flare();
            flare.Time = tempTime;
            flare.Intensity = tempIntensity;
            flare.Altitude = tempAltitude;
            flare.Azimuth = tempAzimuth;
            flare.SatelliteNo = tempSatelliteNo;

            return flare;
        }

        private static String[] getStrings(String flareHTML)
        {
            int iterations = 8;
            int index = 0;
            String[] strings = new String[iterations];
            for (int i = 0; i < iterations; i++)
            {
                match = Regex.Match(flareHTML, RegAttr);
                index = match.Index + match.Groups[1].ToString().Length;
                flareHTML = flareHTML.Substring(index);
                strings[i] = match.Groups[1].ToString();
            }
            return strings;
        }

        private static DateTime parseDateTime(String dateTag, String timeTag)
        {
            int year = DateTime.Now.Year;
            match = Regex.Match(timeTag, RegTime);
            String time = match.Groups["time"].ToString();
            String strDateTime = dateTag + " " + year + " " + time;

            DateTime dateTime = DateTime.Parse(strDateTime);
            return dateTime;
        }

        private static int parseIntensity(String intensityTag)
        {
            int intensity = Int32.Parse(intensityTag);
            return intensity;
        }

        private static int parseSatelliteNo(String satelliteTag)
        {
            match = Regex.Match(satelliteTag, RegSatelliteNo);
            String satelliteNo = match.Groups["sat"].ToString();

            return Int32.Parse(satelliteNo);
        }

        private static int parseDegree(String degreeTag)
        {
            match = Regex.Match(degreeTag, RegDegree);
            String degree = match.Groups["degree"].ToString();

            return Int32.Parse(degree);
        }

        #endregion
    }
}
